﻿using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace NullTodoList
{
    /// <summary>
    /// Форма настроек.
    /// </summary>
    public partial class frmSettings : Form
    {
        private Settings settings;
        /// <summary>
        /// Конструктор по-умолчанию.
        /// </summary>
        public frmSettings(Settings settings)
        {
            InitializeComponent();
            LoadSettings(settings);
        }

        private void LoadSettings(Settings settings)
        {
            this.settings = settings;
            // Алерт.
            chkPlaySound.Checked = settings.Alert.PlaySound;
            edtSoundFile.Text = settings.Alert.SoundFile;
            // SMS.
            edtSMSLogin.Text = settings.SMS.Login;
            edtSMSPassword.Text = settings.SMS.Password;
            edtPhone.Text = settings.SMS.Phone;
            chkTranslit.Checked = settings.SMS.Translit;
            // E-mail.
            edtTo.Text = settings.Mail.To;
            edtFrom.Text = settings.Mail.From;
            edtSmtpHost.Text = settings.Mail.SmtpHost;
            edtSmtpPort.Text = settings.Mail.SmtpPort.ToString();
            edtMailPassword.Text = settings.Mail.Password;
            chkEnableSSL.Checked = settings.Mail.EnableSsl;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog.FileName = edtSoundFile.Text;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
                edtSoundFile.Text = openFileDialog.FileName;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // Алерт.
            settings.Alert.PlaySound = chkPlaySound.Checked;
            settings.Alert.SoundFile = edtSoundFile.Text;
            // SMS.
            settings.SMS.Login = edtSMSLogin.Text;
            settings.SMS.Password = edtSMSPassword.Text;
            settings.SMS.Phone = edtPhone.Text;
            settings.SMS.Translit = chkTranslit.Checked;
            // E-mail.
            Int32 port;
            settings.Mail.To = edtTo.Text;
            settings.Mail.From = edtFrom.Text;
            settings.Mail.SmtpHost = edtSmtpHost.Text;
            settings.Mail.SmtpPort = Int32.TryParse(edtSmtpPort.Text, out port) ? port : 587;
            settings.Mail.Password = edtMailPassword.Text;
            settings.Mail.EnableSsl = chkEnableSSL.Checked;
        }

        private void lblSmsWebsite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var label = sender as LinkLabel;
            if (label != null)
                Process.Start(label.Text);
        }

  
    }
}
